home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 2894 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.4 KB  |  47 lines

  1. Path: colossus.holonet.net!russell
  2. From: russell@news.mdli.com (Russell Blackadar)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Parameterless functions
  5. Date: 20 Jan 1996 01:55:17 GMT
  6. Organization: HoloNet National Internet Access System: 510-704-1058/modem
  7. Message-ID: <4dpi25$vp@colossus.holonet.net>
  8. References: <9601191115.aa06760@paris.ics.uci.edu>
  9. NNTP-Posting-Host: jubal.mdli.com
  10. X-Newsreader: TIN [version 1.2 PL2]
  11.  
  12. Raymond Klefstad, Ph.D. (klefstad@catalina.ICS.UCI.EDU) wrote:
  13.  
  14. : Why does the following function work?  Is this just gcc or is it correct
  15. : C++?  endl and ends are implemented this way.
  16.  
  17. : #include <iostream.h>
  18.  
  19. : ostream& newline(ostream& out)
  20. : {
  21. :     return out << "Hello";
  22. : }
  23.  
  24. : main()
  25. : {
  26. :     cout << "Hello" << newline;
  27. : }
  28.  
  29. Really cool, isn't it?  But it's not magic.  
  30.  
  31. A "parameterless function", i.e. a function name without the (), is
  32. merely a pointer to the function.  In the case above, its type is
  33.   ostream& (*)(ostream&).
  34.  
  35. Look in iostream.h and you will find an appropriate << overload, e.g.
  36.   ostream& operator<<(ostream& (*f)(ostream&)) { return (*f)(*this); }
  37.  
  38. I guess it's almost magic. :)
  39.  
  40. : Also, I noticed the following in some of the standard headers for gcc:
  41. : int foo(int x) return y
  42.  
  43. It's not C++, and I wouldn't want my compiler to accept code like this.
  44. Are you sure this line actually compiles, or is it just a comment?
  45. --
  46. Russell Blackadar,   russell@mdli.com
  47.